<?php
/**
 * 轮播图模型
 */

namespace app\api\model;

use app\common\lib\RespCode;
use custom\Err;
use PDOStatement;
use think\Collection;
use Think\Db;
use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Model;

class Follow extends Model {
    /**
     * 跟进记录列表
     * @param $params
     * @param $pageSize
     */
    public function getFollowList($params,$pageSize){
        $data=db('agent_follow_log')
            ->field('remark,itime')
            ->where("agent_user_id={$params['agent_user_id']}")
            ->page($params['page'],$pageSize)
            ->order('itime desc')
            ->select();
        foreach ($data as $key =>$value){
            $data[$key]['itime']=date('m-d H:i',$value['itime']);
            $data[$key]['remark']=base64_decode($value['remark']);
        }

        $count=db('agent_follow_log')
            ->where("agent_user_id={$params['agent_user_id']}")
            ->count();
        if ($pageSize > 0) {
            $pageNum = ceil($count / $pageSize);
        } else {
            $pageNum = 1;
        }
        $result['count'] = $count;
        $result['page'] = (int)$params['page'];
        $result['pageNum'] = $pageNum;
        $result['data'] = $data;
        return $result;

    }
    /**
     * 添加客户跟进
     * @param $params
     * @return int|string
     */
    public function saveFollow($params){
        $add['remark']=trim($params['remark']);//跟进内容
        $add['agent_user_id']=$params['agent_user_id'];//经纪人与用户关联id
        if (mb_strlen($add['remark'])>50){
            Err::add('跟进内容填写格式错误',RespCode::PARAMS_ERR);
            return false;
        }
        $add['remark']=base64_encode($add['remark']);//跟进内容
        $add['itime']=time();//添加时间
        Db::startTrans();
        $re=db('agent_follow_log')->insert($add);
        if (!$re){
            Err::add('保存失败',RespCode::QUERY_ERR);
            return false;
        }
        //增加互动次数
        $addnum=db('company_agent_user')->where("id={$params['agent_user_id']}")->update(['last_time'=>time(),'num'=>Db::raw('num+1')]);
        if ($addnum===false){
            Db::rollback();
            Err::add('保存失败',RespCode::QUERY_ERR);
            return false;
        }
        Db::commit();
        return true;

    }

}
